<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    width="50%"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      :disabled="mode == 'show'"
      ref="dialogForm"
      label-width="auto"
      label-position="right"
    >
      <el-form-item label="产品编码" prop="goodsNum" v-if="mode != 'add'">
        <!-- <el-input v-model.number="form.goodsNum" clearable></el-input> -->
        <p>{{ form.goodsNum }}</p>
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input
          v-model="form.goodsName"
          clearable
          placeholder="请输入产品名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="客户" prop="customerId">
        <el-select
          v-model="form.customerId"
          placeholder="请选择客户"
          style="width: 100%"
          remote
          filterable
          remote-show-suffix
          :remote-method="queryCustomer"
          :loading="selectCustomerLoading"
          clearable
        >
          <el-option
            v-for="item in customerList"
            :key="item.id"
            :label="item.customerName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产品价格（元）" prop="price" class="priceItem">
        <el-input
          v-model="form.price"
          clearable
          placeholder="请输入产品价格"
          oninput="value=value.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
          @blur="form.price = $event.target.value"
        ></el-input>
      </el-form-item>
      <el-form-item label="产品描述" prop="remark" style="width: 100%">
        <el-input
          v-model="form.remark"
          rows="3"
          clearable
          type="textarea"
          placeholder="请输入产品描述"
        ></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
export default {
  emits: ['success', 'closed'],
  data () {
    return {
      mode: "add",
      titleMap: {
        add: '新增',
        edit: '编辑',
        show: '查看'
      },
      visible: false,
      isSaveing: false,
      selectCustomerLoading: false,
      //表单数据
      form: {
        adminId: '',//跟进人id --登录时id
        departmentId: '',
        customerId: '',
        goodsName: '',//产品名称
        price: '',//产品价格
        remark: '',//产品描述
      },
      customerList: [],
      checkForm: {
        userId: '',
      },
      //验证规则
      rules: {

        goodsName: [
          { required: true, message: '请输入产品名称' }
        ],
        customerId: [
          { required: true, message: '请选择客户', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请输入产品价格' }
        ],
      }
    }
  },
  mounted () {
    this.getCustomerList()
  },
  methods: {
    // 获取客户列表
    async getCustomerList (val) {
      this.selectCustomerLoading = true
      let param = {
        limit: 1000,
        page: 1,
        search: val
      }
      let res = await this.$API.customer.getCustomers.get(param)
      this.selectCustomerLoading = false
      this.customerList = res.data
    },
    // 搜索客户
    queryCustomer (val) {
      this.getCustomerList(val)
    },

    //显示
    open (mode = 'add') {
      this.mode = mode
      this.visible = true
      let userInfo = this.$TOOL.data.get('USER_INFO')
      this.form.departmentId = userInfo.departmentId
      this.form.adminId = userInfo.userId
      return this
    },
    //表单提交方法
    submit () {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let res = null
          console.log(this.form)
          if (this.mode == 'add') {
            res = await this.$API.customer.productManagement.addProduct.post(this.form)
          } else if (this.mode == 'edit') {
            delete this.form.createTime
            delete this.form.updateTime
            res = await this.$API.customer.productManagement.updateProduct.post(this.form)

          }
          this.isSaveing = false

          if (res.code == 200) {
            this.$emit('success', this.form, this.mode)
            this.visible = false
            this.$message.success("操作成功")
          }
        }
      })
    },

    //表单注入数据
    setData (data) {
      this.form = data
    },

  }
}
</script>

<style lang="scss" scoped>
</style>
